package com.ai.zuochengyun.phase01.class03;

public class Code14_PrintBinaryTree {
    public static void printTree(BinaryTreeNode head) {
        System.out.println("Binary Tree");
        printInOrder(head, 0, "H", 17);
    }

    private static void printInOrder(BinaryTreeNode head, int height, String to, int len) {
        if (head == null) {
            return;
        }

        printInOrder(head.right, height+1, "v", len);
        String val = to+head.value+to;

        int lenM = val.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenM- lenL;
        val = getSpace(lenL)+val + getSpace(lenR);
        System.out.println(getSpace(height*len)+val);
        printInOrder(head.left, height+1, "^", len);
    }

    private static String getSpace(int num) {
        String space = " ";
        StringBuilder buf = new StringBuilder("");
        for (int i = 0; i < num; i++) {
            buf.append(space);
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        BinaryTreeNode head = new BinaryTreeNode(1);
        head.left = new BinaryTreeNode(2);
        head.right = new BinaryTreeNode(3);
        head.left.left = new BinaryTreeNode(4);
        head.right.left = new BinaryTreeNode(5);
        head.right.right = new BinaryTreeNode(6);
        head.left.left.right = new BinaryTreeNode(7);
        printTree(head);
    }
}
